package com.tenglong.teacher.ui.setting.setting;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.cuotibao.zhuoxiao.basics.utils.Utils;
import com.cuotibao.zhuoxiao.basics.utils.recyview.DividerSpaceItemDecoration;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tenglong.teacher.R;
import com.tenglong.teacher.services.HelpAndFeedbackInfo;
import com.tenglong.teacher.ui.adapter.MyExpandableAdapter;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class HelpAndFeedbackActivity extends BaseActivity {


    private List<HelpAndFeedbackInfo> infos;
    private RecyclerView mRecyclerView;
    private MyExpandableAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_and_feedback);
        getData();
        initView();
    }
    private void getData() {
        infos = new Gson().fromJson(readFromAsset(), new TypeToken<List<HelpAndFeedbackInfo>>() {
        }.getType());
        infos.add(new HelpAndFeedbackInfo(true));
    }

    private void initView() {
        setTitle("帮助与反馈");
        mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.addItemDecoration(new DividerSpaceItemDecoration(Utils.dip2px(this, 1)));
        adapter = new MyExpandableAdapter(infos);
        mRecyclerView.setAdapter(adapter);
    }

    private String readFromAsset() {
        String val = null;
        InputStream is = null;
        try {
            is = getAssets().open("data/" + "help.json");
            val = readStringFromStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                    is = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return val;
    }

    public static String readStringFromStream(InputStream is) throws IOException {
        return new String(readBytesFromStream(is));
    }

    public static byte[] readBytesFromStream(InputStream is) throws IOException {
        byte[] bytes = new byte[is.available()];
        is.read(bytes, 0, bytes.length);
        is.close();
        return bytes;
    }
}
